<template>
  <div class="app-container">
    <div style="width: 100%;">
      <!-- v-model实现双向绑定,handleFilter事件响应 原生回车事件  -->
      <el-select v-model="domain" placeholder="网站名称">
        <el-option
          v-for="item in domains"
          :key="item.id"
          :label="item.name"
          :value="item.name">
        </el-option>
      </el-select>
      <el-button style="margin-left: 1em" type="primary" icon="el-icon-refresh" @click="refetchData">
        重置
      </el-button>
    </div>
    <el-table
      style="margin-top: 1em"
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column align="center" prop="id" label="轮播图id">
      </el-table-column>
      <el-table-column prop="pic" label="轮播图片">
        <template slot-scope="scope">
          <img :src="scope.row.pic" width="50px" height="50px"/>
        </template>
      </el-table-column>
      <el-table-column prop="domain" label="网站实例">
      </el-table-column>
      <el-table-column prop="update_time" label="更新时间">
      </el-table-column>
      <el-table-column label="操作"
                       width="250px">
      <template slot-scope="scope">
        <el-button @click="editRow(scope.row)">编辑</el-button>
        <el-button type="danger" @click="rowDelete(scope.row)">删除</el-button>
      </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import { getBannerList,deleteBanner,searchBannerList} from '@/api/banner'
import {getList} from '@/api/domain';
import {domainMovieList} from "@/api/movieList";

export default {
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'gray',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      list: null,
      listLoading: true,
      domains:[],
      domain:''
    }
  },
  created() {
    this.fetchData()
    this.getDomains()
  },
  methods: {
    fetchData() {
      this.listLoading = true
      getBannerList().then(response => {
        this.list = response.data
        this.listLoading = false
      })
    },
    editRow(row){
      this.$router.push({ path: `baEdit`,query: {id: row.id}})
    },
    getDomains(){
      getList().then(response => {
        this.domains = response.data
      })
    },
    searchList(){
      let param ={
        'domain':this.domain
      }
      searchBannerList(param).then(response => {
        if(response.data.length > 0) {
          this.list = response.data
        }else{
          this.list = null
        }
      })
    },
    refetchData(){
      this.domain = ''
      this.fetchData()
    },
    rowDelete(row){
      let params = {
        id:row.id
      }
      this.$confirm('确认删除该条记录', '确认', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'success',
        callback: action => {
          if (action === 'confirm') {
            deleteBanner(params).then(response => {
              if(response.data == 1){
                this.$message({
                  showClose: true,
                  message: '删除成功',
                  type: 'success'
                });
                if(this.list.length == 1){
                  this.list.pop()
                }
                this.fetchData()
              }
            })
          }
          else {
            return
          }
        }
      })
    },
    watchdomain(){
      this.searchList()
    }
  },
  watch: {
    domain: 'watchdomain'
  }
}
</script>
